let curr = 1;						//当前页数
let pageNum = 0;					//总共几页
let query_item_name = "";			// 名字
let query_item_state = "";			// 状态

// let size = Number($('.select_page').val());		//每页显示几条数据

let app = new Vue({
	el:'#app',
	data:{
		total: 0,
		size: 10,
		curr: 1,
		list:[],
		display:false,
		mod_displear:false,
		
		ckall:false,
		idzhi:[],
		
		add_item_name:"",
		add_item_price:'',
		add_item_format:'',
		add_item_total:'',
		add_item_total:'',
		add_item_inventory:'',
		
		item_name:"",
		item_price:"",
		item_format:"",
		item_total:"",
		item_inventory:"",
		
		modify_id:'',
		modify_name:'',
		modify_price:'',
		modify_total:'',
		modify_format:'',
		modify_inventory:'',
		
		inp:'',
		stock:'',
		
		
		
	},
	computed:{
		pagenum:function(){
			this.curr = 1;
			return Math.ceil(this.total / this.size);
		}
	},
	
	created:function(){
		this.getList()
	},
	methods:{
		getCurr:function(curr){
			this.curr = curr;
			this.getList();
		},
		
		changeSize:function(){
			console.log(this.size);
			this.curr = 1;
			this.getList();
		},
		
		getList: function(){
			axios.get('http://chonghekj.com/crud/index.php/Home/Index/getItemList', {
				params: {
					pageSize: this.size,
					pageNumber: this.curr,
					item_name: query_item_name,
					item_state: query_item_state,
				}
			})
			.then((res)=>{
				// console.log(res);
				this.list = res.data.rows;
				this.total = res.data.total;
			})
		},
		
		addItem:function(){
			this.display=true;
		},
		
		add_miss:function(){
			this.display=false;
		},
		
		add_qu:function(){
			this.display=false;
		},
		
		add_ti:function(){
			this.display=false;
			axios.post('http://chonghekj.com/crud/index.php/Home/Index/addItem',Qs.stringify({
					item_name:this.add_item_name,
					item_price:this.add_item_price,
					item_format:this.add_item_format,
					item_total:this.add_item_total,
					item_inventory:this.add_item_inventory,
			}))
			.then((res) =>{
				this.curr = 1;
				this.getList();
				this.add_item_name = "";
				this.add_item_price = "";
				this.add_item_format = "";
				this.add_item_total = "";
				this.add_item_inventory = "";
			})
		},
		
		deleteItem:function(){
			console.log(this);
			if(this.idzhi.length >= 1){
				let con = confirm("是否要删除当前"+this.idzhi.length+"数据");
				if(con){
					axios.post('http://chonghekj.com/crud/index.php/Home/Index/deleteItem',Qs.stringify({
						ids:this.idzhi,
					}))
					.then((res) => {
						this.getList();
					})
					this.idzhi = [];
				}
			}
			
		},
		
		updata:function(){
			// console.log(this.idzhi.length);
			if(this.idzhi.length == 1){
				this.mod_displear=true;
			}else if(this.idzhi.length >1 ){
				alert('多选改个屁！');
				this.idzhi = [];
			}else{
				alert('选完了再去改，不选改个屁！');
			}
		},
		
		mod_miss:function(){
			this.mod_displear = false;
		},
		
		cancel1:function(){
			console.log(1)
			this.mod_displear = false;
		},
		
		modItem:function(item){
			console.log(item);
			this.item_id = item.item_id;
			// console.log(this.item_id);
			this.item_name = item.item_name;
			this.item_price = item.item_price;
			this.item_format= item.item_format;
			this.item_total= item.item_total;
			this.item_inventory= item.item_inventory;
			
			this.modify_name = this.item_name;
			// console.log(this.modify_name);
			this.modify_price = this.item_price;
			this.modify_format = this.item_format;
			this.modify_total = this.item_total;
			this.modify_inventory = this.item_inventory;
			
			if(this.idzhi.length == this.list.length){
				this.ckall = true;
			}else{
				this.ckall = false;
			}
		},
		
		submit1:function(){
			// console.log(this.idzhi)
			axios.post('http://chonghekj.com/crud/index.php/Home/Index/editItem',Qs.stringify({
				item_id:this.item_id,
				// item_price:this.modify_price,
				item_name:this.modify_name,
				item_format:this.modify_format,
				item_total:this.modify_total,
				item_inventory:this.modify_inventory,
			}))
			.then((res) =>{
				this.getList();
				this.mod_displear = false;
			})
		},
		
		query:function(){
			query_item_name = this.inp;
			query_item_state = this.stock;
			
			this.getList();
			this.inp = '';
		},
		
		ckallItem:function(){
			console.log(this.ckall);
			if(this.ckall){
				this.idzhi = this.list.map(function(val){
					return val.item_id;
				})
			}else{
				this.idzhi = [];
			}
		}
	},
	
})
